package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.Service.UserService;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if(user == null) {
            return null;
        }
        Set<Role> roles = roleDao.findRolesByUserId(user.getId());
        if(roles != null && roles.size() >0){
             for (Role role : roles) {
                 Set<Permission> permissions = permissionDao.findPermissionsByRoleId(role.getId());
                 role.setPermissions(permissions);
             }
        }
        user.setRoles(roles);
        return user;

    }
}
